<?php
namespace app\index\controller;

use think\Request;
use app\common\controller\HomeBase;
use app\common\model\ConfigComm;
use app\common\model\WebVipComm;
use app\index\model\Index as homeIndexModel;
use app\index\model\Comment as homeCommentModel;


class Index extends HomeBase
{

    ///ip每日限制评论数量
    private $ipCommentLimit;

    public function __construct(ConfigComm $configComm)
    {
        parent::__construct();
        $this->ipCommentLimit = config('admin.ipCommentLimit',10);

        ///赋值公共变量
        $this->assign([
            'web_common_logs_arr'=>array(///网站log地址
                '/static/otaku/otd/images/log/default_log1.jpg',
                '/static/otaku/otd/images/log/default_log2.ico',
                '/static/otaku/otd/images/log/default_log3.jpg',
                '/static/otaku/otd/images/log/default_log4.png'
            ),
            'web_common_logs_number'=>4,///网站log数量
        ]);
        $this->assign($configComm->bathVar());
    }

    ///首页
    public function index(homeIndexModel $indexModel,WebVipComm $webVipComm)
    {
        ///调用页面
        $this->assign([
            'index_data_arr'=>$indexModel->getIndexData(),
            'all_web_vip_data'=>$webVipComm->getAllWebVipData(),
        ]);
        return view();
    }

    ///联系我们
    public function message()
    {
        $this->assign(['web_comm_only_domain'=>request()->domain()]);
        return view();
    }


    ///留言
    public function links(homeCommentModel $homeCommentModel){
        ///获取页面cookie
        $this->assign([
            'hxcdh_web_msg_title'=> cookie('nav_pro_msg_title') ?? '默认',
            'hxcdh_web_msg_email'=> cookie('nav_pro_msg_email') ?? ''
        ]);
        ///调用页面
        $this->assign(['top_comment_data'=>$homeCommentModel->getCommentData()]);
        return view();

    }

    ///提交留言
    public function contact(Request $request,homeCommentModel $homeCommentModel){
        ///过滤参数
        $com_title = $request->param('title','','cus_character_filter');
        $com_email = $request->param('email','');
        $com_content = $request->param('content','','cus_character_filter');
        $com_ip = $request->ip();

        ///内容只能为有效数据&&只能ajax请求&&每个ip一天只能评论10次
        if (ctype_alnum($com_content) || in_array($com_content, ['test', '测试'])){return think_json_encode(['status'=>2,'msg'=>'禁止无意义评论！']);}
        if(!$request->isAjax()){return think_json_encode(['status'=>2,'msg'=>'留言已关闭，带来不便，请谅解！']);}
        if($homeCommentModel->getThisIpCommentLimit($com_ip)>$this->ipCommentLimit){return think_json_encode(['status'=>2,'msg'=>'留言已关闭，带来不便，请谅解！']);}

        ///邮箱&网站标题存储到cookie
        setcookie('nav_pro_msg_title',$com_title,time()+3600*24*7,'/','',false,true);
        setcookie('nav_pro_msg_email',$com_email,time()+3600*24*7,'/','',false,true);

        ///评论写入数据库
        $homeCommentModel->insNewComment(['com_title' => $com_title, 'com_email' => $com_email,'com_content'=>$com_content,'com_ip'=>$com_ip]);
        return think_json_encode(['status'=>0,'msg'=>'留言成功！']);
    }


    ///更多
    public function webMore(Request $request,homeIndexModel $homeIndexModel){
        $c_id = $request->param('c_id',0,'intval');
        $c_tit = $request->param('c_tit','珍贵收藏');

        $this->assign([
            'class_more_data'=>$homeIndexModel->getDesClassData($c_id),
            'c_tit'=>$c_tit,
        ]);
        return view();
    }


    ///网站详情
    public function webDetails(Request $request,homeIndexModel $homeIndexModel){
        $w_id = $request->param('w_id',0,'intval');

        $this->assign([
            'web_details'=>$homeIndexModel->getWebDetails($w_id),
        ]);
        return view();
    }







}
